package ATBMS.ucm;

import ATBMS.control.EditUserControl;
import ATBMS.control.MainApp;
import ATBMS.modles.DAO.DAOFactory;
import ATBMS.modles.DAO.TicketDAO;
import ATBMS.modles.DAO.UserDAO;
import ATBMS.modles.User;
import ATBMS.view.user.UserViewInterface;

/**
 * Edit User Use Case Implement
 */
public class UCEditUser implements UCController {

    private EditUserControl euc;
    User userNeedToEdit;

    public UCEditUser(EditUserControl euc, User user) {
        this.euc = euc;
        this.userNeedToEdit = user;
    }

    public void run() {
        User editedUser = euc.getUserEditView().getInput();

        if (editedUser != null) {
            String checkRet = checkUser(editedUser);
            if (checkRet != null) {
                euc.getUserEditView().alert(checkRet);
                return;
            }


            UserDAO userdao = DAOFactory.getUserDAO();
            if (userdao == null) {
                euc.getUserEditView().alert("Can't Connect To DB!");
                return;
            }
            if (userdao.update(userNeedToEdit.getUid(), editedUser)) {
                euc.getUserEditView().alert("Edit successed!");
                userNeedToEdit.setUaddr(editedUser.getUaddr());
                userNeedToEdit.setUemail(editedUser.getUemail());
                userNeedToEdit.setUtel(editedUser.getUtel());

                TicketDAO ticketdao = DAOFactory.getTicketDAO();
                if (ticketdao != null) {
                    ticketdao.updateUsersTelphone(
                            userNeedToEdit.getUid(),
                            userNeedToEdit.getUtel());

                   
                }
            } else {
                euc.getUserEditView().alert("Edit failed!");
            }

            /*
            UserDAO userdao = DAOFactory.getUserDAO();
            if (userdao == null) {
            euc.getUserEditView().alert("Can't Connect To DB!");
            return;
            }
            if (!userdao.update(userNeedToEdit.getUid(), newUser)) {
            euc.getUserEditView().alert(userdao.getErrorMessage());
            } else {
            euc.getUserEditView().alert("  Congratulations " + newUser.getUname() + " !\r\n" +
            "Register successed!Please login.");
            }
             * */
        } else {
            euc.getUserEditView().alert("Internal Error");
            MainApp.getApplication().exit();
        }
    }

    static private String checkUser(User user) {

        if (user.getUname().length() == 0) {
            return ("User Name can't be null!");
        }
        if (user.getPassword().compareTo(user.getPassword2()) != 0) {
            return ("Two password is not same!");
        }
        if (user.getUaddr().length() == 0) {
            return ("Address can't be null!");
        }
        if (user.getUemail().length() == 0) {
            return ("E-Mail can't be null!");
        }
        if (user.getUtel().length() == 0) {
            return ("Telephone can't be null!");
        }
        return null;
    }
}
